package dao;

import dbutil.DBHelp;
import vo.Course;
import vo.Student;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ssw on 2018/6/16.
 */
public class StudentDao {
    public  List<Student> askStudentList(String clname) {
        DBHelp dh=new DBHelp();
        List<Student> studentList=new LinkedList<>();
        String sql="select students.sid,users.uname,students.clname\n" +
                "from students,users\n" +
                "where students.sid=users.uid and clname=?";
        try {
            ResultSet rs=dh.query(sql,clname);
            while(rs.next()){
                studentList.add(new Student(rs.getString(1),rs.getString(2),rs.getString(3)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return studentList;
    }

    public List<Student> askAllStudentList() {
        DBHelp dh=new DBHelp();
        List<Student> studentList=new LinkedList<>();
        String sql="select students.sid,users.uname,students.clname\n" +
                "from students,users\n" +
                "where students.sid=users.uid ";
        try {
            ResultSet rs=dh.query(sql);
            while(rs.next()){
                studentList.add(new Student(rs.getString(1),rs.getString(2),rs.getString(3)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return studentList;
    }
}
